#region The MIT License
// The MIT License
// 
// Copyright (c) 2009 octalforty studios
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

namespace octalforty.Componento.Shared.SemanticModel
{
    [DebuggerDisplay("Cli: {Cli}, Isa: {Isa}")]
    public sealed class PathMapping : IEquatable<PathMapping>, IEnumerable<PathMappingTarget>
    {
        #region Private Fields
        private readonly IDictionary<PathMappingTarget, string> mappings = new Dictionary<PathMappingTarget, string>();
        private readonly CliEnvironment cli;
        private readonly IsaType isa;
        #endregion

        #region Public Properties
        public string this[PathMappingTarget mappingTarget]
        {
            [DebuggerStepThrough]
            get { return mappings[mappingTarget]; }
        }

        public CliEnvironment Cli
        {
            [DebuggerStepThrough]
            get { return cli; }
        }

        public IsaType Isa
        {
            [DebuggerStepThrough]
            get { return isa; }
        }
        #endregion

        public PathMapping(CliEnvironment cli, IsaType isa)
        {
            this.cli = cli;
            this.isa = isa;
        }

        public void AddPathMap(PathMappingTarget mappingTarget, string path)
        {
            mappings[mappingTarget] = path;
        }

        public void Add(PathMappingTarget mappingTarget, string path)
        {
            AddPathMap(mappingTarget, path);
        }

        #region IEnumerable<PathMappingTarget> Members
        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public IEnumerator<PathMappingTarget> GetEnumerator()
        {
            foreach(PathMappingTarget mappingTarget in mappings.Keys)
                yield return mappingTarget;
        }
        #endregion


        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the other parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(PathMapping other)
        {
            if(ReferenceEquals(null, other)) return false;
            if(ReferenceEquals(this, other)) return true;
            return Equals(other.cli, cli) && other.isa.Equals(isa);
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param><filterpriority>2</filterpriority>
        public override bool Equals(object obj)
        {
            if(ReferenceEquals(null, obj)) return false;
            if(ReferenceEquals(this, obj)) return true;
            if(obj.GetType() != typeof(PathMapping)) return false;
            return Equals((PathMapping)obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type. <see cref="M:System.Object.GetHashCode"/> is suitable for use in hashing algorithms and data structures like a hash table.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override int GetHashCode()
        {
            unchecked
            {
                return ((cli != null ? cli.GetHashCode() : 0) * 397) ^ isa.GetHashCode();
            }
        }

        public override string ToString()
        {
            return string.Format("Cli: {0}, Isa: {1}", cli, isa);
        }
    }
}